package com.wk1un2.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wk1un2.entity.User;
import com.wk1un2.service.OrderFormService;
import com.wk1un2.service.ShoppingCartService;
import com.wk1un2.service.UserReAddressService;
import com.wk1un2.util.Result;
import com.wk1un2.service.OrderDetailsService;

@Controller
public class ShoppingCartWebController {

	@Autowired
	ShoppingCartService ShoppingCartService;
	@Autowired
	OrderFormService OrderFormService;
	@Autowired
	OrderDetailsService OrderDetailsService;
	@Autowired
	UserReAddressService UserReAddressService;

	@RequestMapping("/Front/sCartAdd")
	public String sCartAdd(Model model, HttpSession session, @RequestParam Map<String, Object> map) {
		User u = (User) session.getAttribute("USER_SESSION");
		if (u != null) {
			System.out.println(map);
			map.put("u_id", u.getU_id());
			int i = ShoppingCartService.add(map);
			return "redirect:sCartGet";
		}
		return "forward:/Front/login.jsp";
	}

	@RequestMapping("/Front/sCartGet")
	public String sCartGet(Model model, HttpSession session) {
		Integer u_id = ((User) session.getAttribute("USER_SESSION")).getU_id();
		if (u_id != null) {
			List<Map<String, Object>> getCart = ShoppingCartService.getAll(0, 9999, u_id,null);
			System.out.println(u_id);
			model.addAttribute("getCart", getCart);
			// 重定向到主页面的跳转方法
			return "ShoppingCart";
		}
		return "forward:/Front/login.jsp";
	}

	@RequestMapping("/Front/sCartEdit")
	public String sCartEdit(Model model, HttpSession session, @RequestParam Map<String, Object> map) {
		User u = (User) session.getAttribute("USER_SESSION");
		map.put("u_id", u.getU_id());
		int i = ShoppingCartService.update(map);
		return "";
	}

	@RequestMapping("/Front/sCartDel")
	public String sCartDel(Model model, HttpSession session, @RequestParam Map<String, Object> map) {
		User u = (User) session.getAttribute("USER_SESSION");
		map.put("u_id", u.getU_id());
		int i = ShoppingCartService.delete(map);
		return "redirect:sCartGet";
	}

	@RequestMapping("/Front/GetOrderForm")
	public String GetOrderForm(Model model, HttpSession session,String sc_id) {
		Integer u_id = ((User) session.getAttribute("USER_SESSION")).getU_id();
		if (u_id != null) {
			List<Map<String, Object>> getCart = ShoppingCartService.getAll(0, 9999, u_id,sc_id);
			List<Map<String, Object>> getAddress = UserReAddressService.getAll(u_id);
			model.addAttribute("getCart", getCart);
			model.addAttribute("getAddress", getAddress);
			// 重定向到主页面的跳转方法
			return "ShoppingCartdetails";
		}
		return "forward:login.jsp";
	}

	@RequestMapping("/Front/OrderFormAdd")
	@ResponseBody
	public Object OrderFormAdd(Model model, HttpSession session, @RequestParam Map<String, Object> map) {
		System.out.println(map);
		User u = (User) session.getAttribute("USER_SESSION");
		String oid = new Date().getTime() + "";
		System.out.println(oid);
		if (u != null) {
			ObjectMapper mapper = new ObjectMapper();
			map.put("u_id", u.getU_id());
			map.put("of_id", oid);
			JSONArray js = JSON.parseArray(map.get("pros").toString());
			map.put("of_num", js.size());
			int i = OrderFormService.addOrderForm(map);
			for (Object object : js) {
				Map m = (Map) object;
				m.put("of_id", oid);
				m.put("u_id", u.getU_id());
				System.out.println(m);
				OrderDetailsService.addOrderForm(m);
			}
			Result res = new Result(0, "", i, map);
			return res;
		}
		return "forward:login.jsp";
	}

	@RequestMapping("/Front/OrderFormId")
	public String OrderFormId(Model model, HttpSession session, @RequestParam Map<String, Object> map) {
		User u = (User) session.getAttribute("USER_SESSION");
		if (u != null) {
			map.put("u_id", u.getU_id());
			List<Map<String, Object>> getOrderId = OrderFormService.getId(map);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			model.addAttribute("getOrderId", getOrderId);
			return "ShoppingCartsubmit";
		}
		return "forward:login.jsp";
	}
	@RequestMapping("/Front/payId")
	public String payId(Model model,HttpSession session,@RequestParam Map<String, Object> map) {
		User u=(User) session.getAttribute("USER_SESSION");
		if (u!=null) {
			map.put("u_id", u.getU_id());
			List<Map<String, Object>> getOrderId=OrderFormService.getId(map);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			System.out.println(getOrderId);
			model.addAttribute("getOrderId", getOrderId);
			return "alipay/index";
		} 
		return "forward:login.jsp";
	}
}
